package cn.glc.daily.life.web.controller.manageapi;

import cn.glc.daily.life.common.constans.Developers;
import cn.glc.daily.life.common.utils.JsonResult;
import cn.glc.daily.life.model.dto.SendCodeDto;
import cn.glc.daily.life.web.service.captcha.abs.AbstractSendCode;
import cn.glc.daily.life.web.service.user.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 用户相关
 *
 * @author simon
 * @date 2020/5/26 10:00
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private Map<String, AbstractSendCode> absSendCode;

    /**
     * 注册用户 -> 发送短信验证码/邮箱验证码
     *
     * @param sendCodeDto
     * @return
     */
    @GetMapping(value = "/sendCode")
    @ApiOperation(value = "发送验证码", notes = Developers.CY)
    public JsonResult sendCode(@Valid SendCodeDto sendCodeDto) {
        try {
            return JsonResult.of(absSendCode.get(sendCodeDto.getCaptchaType().name())
                    .process(sendCodeDto));
        } catch (Exception e) {
            return JsonResult.of(e);
        }
    }

    /**
     * 查询用户所有详情信息
     *
     * @param uid
     * @return
     */
    @RequiresPermissions("user:query")
    @ApiOperation(value = "查询用户详情", notes = Developers.CY)
    @GetMapping(value = "/findDetails")
    public JsonResult findDetails(@NotNull(message = "标识不为空") @Valid Long uid) {
        return JsonResult.returnSuccess(userService.findDetailsByUid(uid));
    }

    /**
     * @param uid
     * @return
     */
    @PostMapping(value = "/updateInfoByUid")
    @ApiOperation(value = "更新用户信息", notes = Developers.CY)
    public JsonResult updateInfoByUid(@NotNull(message = "标识不为空") @Valid Long uid) {
        return JsonResult.returnSuccess(userService.findDetailsByUid(uid));
    }

    /**
     * 注销用户
     *
     * @param uId
     * @return
     */
    @PostMapping(value = "/accountClose")
    @ApiOperation(value = "注销用户", notes = Developers.CY)
    public JsonResult accountClose(@Valid @NotNull(message = "标识不为空") Long uId) {
        return JsonResult.returnSuccess(userService.accountClose(uId));
    }

}
